package com.offcn.sellergood.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.offcn.sellergood.dao.SpecificationMapper;
import com.offcn.sellergood.dao.SpecificationOptionMapper;
import com.offcn.sellergood.entity.SpecificationEntity;
import com.offcn.sellergood.pojo.Specification;
import com.offcn.sellergood.pojo.SpecificationOption;
import com.offcn.sellergood.service.SpecificationService;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/****
 * @Author:ujiuye
 * @Description:Specification业务层接口实现类
 * @Date 2021/2/1 14:19
 *****/
@Transactional
@Service
public class SpecificationServiceImpl extends ServiceImpl<SpecificationMapper, Specification> implements SpecificationService {

    @Autowired(required = false)
    private SpecificationOptionMapper optionMapper;

    @Override
    public void add(SpecificationEntity entity) {
        // 规格表的数据
        Specification specification = entity.getSpecification();
        this.save(specification);
        Long id = entity.getSpecification().getId();
        //规格选项表
        List<SpecificationOption> options = entity.getOptions();
        if (options != null && options.size()>0){
            for (SpecificationOption option:options){
                option.setSpecId(id); //将插入规格表的id 添加到option中
                optionMapper.insert(option);
            }
        }
    }

    @Override
    public SpecificationEntity findById(Long id) {
        //先查规格
        Specification specification = this.getById(id);
        //差规格选项
        QueryWrapper<SpecificationOption> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spec_id",id);
        List<SpecificationOption> options = optionMapper.selectList(queryWrapper);
        return new SpecificationEntity(specification,options);
    }

    @Override
    public void update(SpecificationEntity entity) {
        //1.修改规格名称对象
        this.updateById(entity.getSpecification());
        //2.根据ID删除规格选项集合
        Long sid = entity.getSpecification().getId();
        QueryWrapper<SpecificationOption> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spec_id",sid);
        //执行删除
        optionMapper.delete(queryWrapper);
        //3.重新插入规格选项
        List<SpecificationOption> options = entity.getOptions();
        if (options != null && options.size()>0){
            for (SpecificationOption option:options){
                option.setSpecId(sid);// 将刚插入的 规格表 的Id 添加到 option 中
                optionMapper.insert(option);
            }
        }
    }

    @Override
    public void deleteBy(Long id) {

        this.removeById(id);
        QueryWrapper<SpecificationOption> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spec_id",id);
        optionMapper.delete(queryWrapper);
    }


}
